#include "_my_ctrl.h"
////////////////////////////////////////////////////////////////////////////////
void ControllerEnhanced::buttons_status_monitoring_routine(
    void *p_this_controller) {
  ControllerEnhanced *p_this_controller_obj =
      (ControllerEnhanced *)p_this_controller; //遥控器指针变量指针类型转换
  while (1) {
    for (auto btn : p_this_controller_obj->vec_ptr_all_buttons) {
      if (btn->additional_function_check_started) { //附加功能监测开启
        if (btn->pressing()) {                      //按键按下
          if (btn->btn_pressed_just_now == false) { //判断是否在上升沿
            btn->btn_pressed_just_now = true;       //上升沿标志置位
            btn->btn_released_just_now = false;     //下降沿标志复位
            /////////////////////////////算法判断
            //上升沿时刻判断：三击
            if (timer::system() - btn->last_click_time <
                    btn->triple_click_max_interval &&
                btn->last_click_time - btn->last_last_click_time <
                    btn->triple_click_max_interval) {
              btn->triple_clicked_status = true;
              btn->double_clicked_status = false;
            } else if (timer::system() - btn->last_click_time <
                       btn->double_click_max_interval) //上升沿时刻判断：双击
              btn->double_clicked_status = true;

            //更新记录上升沿时间
            btn->last_last_click_time = btn->last_click_time;
            btn->last_click_time = timer::system();
          } //在上升沿

          //按着状态判断
          //长按着判断
          if (timer::system() - btn->last_click_time >
              btn->long_pressing_min_time)
            btn->long_pressing_status = true;
          else
            btn->long_pressing_status = false;

          //短按着判断
          if (timer::system() - btn->last_click_time <
              btn->short_pressing_max_time)
            btn->short_pressing_status = true;
          else
            btn->short_pressing_status = false;

          //按过判断
          if (!btn->btn_long_pressed_latch_set &&
              timer::system() - btn->last_click_time >
                  btn->long_pressed_latch_min_time) {
            btn->btn_long_pressed_latch_set = true;
            btn->long_pressed_latch_status = true;
          }

          ///////////////////////////////////////////////////////////////////////////
        } else {                                     //松开按键了
          if (btn->btn_released_just_now == false) { //判断是否是下降沿
            btn->btn_released_just_now = true;       //下降沿标志置位
            btn->btn_pressed_just_now = false;       //上升沿标志复位
            btn->btn_long_pressed_latch_set = false;
            ////////////////////////////////////////
            //短按过的判断
            if (timer::system() - btn->last_click_time <
                    btn->short_pressed_ever_max_time) 
              btn->short_pressed_ever_status = true;
            
          } //下降沿
          //复位按下的状态
          btn->short_pressing_status = false;
          btn->long_pressing_status = false;
        } //按键松开
      }   //附加功能监测开启
    }     // 12个按键循环
    wait(50, msec);
  }
}
//////////////////////////////////////////////////////////////////////////
void ControllerEnhanced::buttons_status_monitoring_start_once() {
  //开启检测线程
  if (!pressing_status_monitoring_on) {
    pressing_status_monitoring_on = true;
    thread t(buttons_status_monitoring_routine, this);
  }
}

//读取后清零
bool ControllerEnhanced::button_enhanced::double_clicked(
    int double_click_max_interval) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->double_click_max_interval = double_click_max_interval;
  if (double_clicked_status == true) {
    double_clicked_status = false;
    return true;
  } else
    return false;
}

//读取后清零，三击包含了2个双击
bool ControllerEnhanced::button_enhanced::triple_clicked(
    int triple_click_max_interval) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->triple_click_max_interval = triple_click_max_interval;
  if (triple_clicked_status == true) {
    triple_clicked_status = false;
    return true;
  } else
    return false;
}

bool ControllerEnhanced::button_enhanced::short_pressed_ever(
    int short_pressed_ever_max_time) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->short_pressed_ever_max_time = short_pressed_ever_max_time;
  if (short_pressed_ever_status) {
    short_pressed_ever_status = false;
    return true;
  }
  return false;
}

bool ControllerEnhanced::button_enhanced::long_pressed_latch(
    int long_pressed_latch_min_time) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->long_pressed_latch_min_time = long_pressed_latch_min_time;
  if (long_pressed_latch_status) {
    long_pressed_latch_status = false;
    return true;
  }
  return false;
}

bool ControllerEnhanced::button_enhanced::short_pressing(
    int short_pressing_max_time) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->short_pressing_max_time = short_pressing_max_time;
  return short_pressing_status;
}

bool ControllerEnhanced::button_enhanced::long_pressing(
    int long_pressing_min_time) {
  p_parent->buttons_status_monitoring_start_once();
  this->additional_function_check_started = true;
  this->long_pressing_min_time = long_pressing_min_time;
  return long_pressing_status;
}
